<?php
/**
 * 角色模块控制器
 * @Author    紫梦
 * @DateTime  2018-01-29
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;

use think\Request;
use app\common\model\NodeGrop;
use app\common\model\Roles as RolesModel;
use app\common\model\RolesNode;
use app\common\model\RolesMenu;
class Roles extends Base
{
	/**
	 * 角色列表
	 */
	public function index()
	{


		$model = RolesModel::all();
		
		$this->assign('data',$model);

		return $this->fetch();	
	}

	/**
	 * 添加
	 */
	public function add()
	{
		$request = Request::instance();
		if($request->isAjax()){
			$msg['status'] = 0;
			$data = $request->post();
			$model = new RolesModel();
				
			try{
			   
				$model->allowField(true)->save($data);

			}catch(\Exception $e){
			    $msg['status'] = 1;
				$msg['msg'] = '添加失败,请稍后再试';
				return $msg;
			}

			foreach ($data['node'] as $key => $value) {
				$role[$key]['role_id'] = $model->id;
				$role[$key]['node_id'] = $value;

			}

			$rolesNode = new RolesNode;
			$roleRes = $rolesNode->saveAll($role);

			foreach (array_merge($data['node_title'],$data['grop_name']) as $key => $value) {
				$menu[$key]['role_id'] = $model->id;
				$menu[$key]['menu_id'] = $value;

			}
 			
			$rolesMenu = new RolesMenu;
			$menuRes = $rolesMenu->saveAll($menu);
			if($roleRes && $menuRes){
				$msg['status'] = 0;
				$msg['msg'] = '添加成功';
				
			}else{

			    $msg['status'] = 1;
				$msg['msg'] = '添加失败,请稍后再试';
			}

			return $msg;
		}else{
			$model = NodeGrop::all(['pid'=>0]);
			$this->assign('data',$model);
			return $this->fetch();

		}

	}

	/**
	 * 编辑
	 */
	public function edit($id=1)
	{
		$request = Request::instance();
		if($request->isAjax()){
			$data = $request->post();			
			$model = new RolesModel();	
			try{
			   
				$model->allowField(true)->isUpdate(true)->save($data);

			}catch(\Exception $e){
			    return [
					'code'=>2,
					'msg'=>$e->getMessage(),
				];
			}
			$rolesNode = new RolesNode;			
			RolesNode::where(['role_id'=>$data['id']])->delete();
			if(!empty($data['node'])){							
				foreach ($data['node'] as $key => $value) {
					$role[$key]['role_id'] = $model->id;
					$role[$key]['node_id'] = $value;
				}

				try{			   
					$rolesNode->saveAll($role);
				}catch(\Exception $e){
				    return [
						'code'=>2,
						'msg'=>$e->getMessage(),
					];
				}
			}

			RolesMenu::where(['role_id'=>$data['id']])->delete();
			if(!empty($data['node_title']) && !empty($data['grop_name'])){
				foreach (array_merge($data['node_title'],$data['grop_name']) as $key => $value) {
					$menu[$key]['role_id'] = $model->id;
					$menu[$key]['menu_id'] = $value;

				}
	 			
				$rolesMenu = new RolesMenu;
				try{

					$menuRes = $rolesMenu->saveAll($menu);

				}catch(\Exception $e){
					return [
						'code'=>2,
						'msg'=>$e->getMessage(),
					];
				}
			}
			
			return [
					'code'=> 1,
					'msg'=> "更新成功"
				];
		}else{
			$model = NodeGrop::all(['pid'=>0]);
			$roles = RolesModel::get($id);
			$roles_node = RolesNode::where(['role_id'=>$id])->column('node_id');
			$roles_menu = rolesMenu::where(['role_id'=>$id])->column('menu_id');
			$menu = NodeGrop::where(['pid'=>0])->where('id','in',$roles_menu)->column('id');
			$grop = NodeGrop::where('pid','<>',0)->where('id','in',$roles_menu)->column('id');
			// dump(in_array(2, $roles_node));
			$this->assign('data',$model);
			$this->assign('roles',$roles);
			$this->assign('roles_node',$roles_node);
			$this->assign('menu',$menu);
			$this->assign('grop',$grop);
			return $this->fetch();

		}

	}	

    public function delete($id)
    {
    	
            $rolesNode = new RolesNode;			
    	try {
            RolesModel::destroy($id);
			RolesNode::where(['role_id'=>$id])->delete();
            
        } catch (\Exception $e) {
            return [
                'code'=>2,
                'msg'=>$e->getMessage()
                ];
            
        }
        
        return [
            'code'=>1,
            'msg'=>'删除成功'
            ];
    }

}
